package com.atguigu.jxc.controller;


import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.service.GoodsTypeService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goodsType")
public class GoodsTypeController {

    @Autowired
    private GoodsTypeService goodsTypeService;

    //查询商品所有分类
    //http://localhost:8080/goodsType/loadGoodsType
//    @PostMapping("loadGoodsType")
//    public String loadGoodsType(){
//        return goodsTypeService.loadGoodsType();
//    }
    @PostMapping("/loadGoodsType")
    public String loadGoodsType(){
        List<Map<String, Object>> maps = goodsTypeService.loadGoodsType(-1);
        //把list转为string
        Gson gson = new Gson();
        String json = gson.toJson(maps);
        System.out.println(json);
        return json;
    }

    //http://localhost:8080/goodsType/save
    //新增分类
    @PostMapping("/save")
    public ServiceVO save(String goodsTypeName, Integer pId) {
        try {
            goodsTypeService.save(goodsTypeName, pId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    //http://localhost:8080/goodsType/delete
    //删除分类
    @PostMapping("/delete")
    public ServiceVO delete(Integer  goodsTypeId) {
        try {
            goodsTypeService.delete(goodsTypeId);
            return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }
}
